package info

import (
	"encoding/json"
	"strconv"
	"time"

	"github.com/yusufpapurcu/wmi"
	"gopkg.in/ini.v1"
)

type Win32_DesktopMonitor struct {
	Availability                uint16    `json:"可用性"`
	Bandwidth                   uint32    `json:"带宽(兆赫)"`
	Caption                     string    `json:"说明"`
	ConfigManagerErrorCode      uint32    `json:"配置管理器错误码"`
	ConfigManagerUserConfig     bool      `json:"是否用户定义配置"`
	CreationClassName           string    `json:"ClassName"`
	Description                 string    `json:"描述"`
	DeviceID                    string    `json:"设备ID"`
	DisplayType                 uint16    `json:"显视器种类"`
	ErrorCleared                bool      `json:"错误"`
	ErrorDescription            string    `json:"错误详细信息"`
	InstallDate                 time.Time `json:"安装日期"`
	IsLocked                    bool      `json:"是否锁定"`
	LastErrorCode               uint32    `json:"最后一个错误代码"`
	MonitorManufacturer         string    `json:"制造商"`
	MonitorType                 string    `json:"监视器类型"`
	Name                        string    `json:"名称"`
	PixelsPerXLogicalInch       uint32    `json:"水平(x轴)分辨率"`
	PixelsPerYLogicalInch       uint32    `json:"垂直(y轴)分辨率"`
	PNPDeviceID                 string    `json:"win即插即用逻设备标识符"`
	PowerManagementCapabilities []uint16  `json:"电源相关"`
	PowerManagementSupported    bool      `json:"电源管理"`
	ScreenHeight                uint32    `json:"逻辑高度"`
	ScreenWidth                 uint32    `json:"逻辑宽度"`
	Status                      string    `json:"状态"`
	StatusInfo                  uint16    `json:"逻辑设备状态"`
	SystemCreationClassName     string
	SystemName                  string `json:"范围系统名称"`
}

type CIM_DesktopMonitor struct {
	Caption                     string
	Description                 string
	InstallDate                 time.Time
	Name                        string
	Status                      string
	Availability                uint16
	ConfigManagerErrorCode      uint32
	ConfigManagerUserConfig     bool
	CreationClassName           string
	DeviceID                    string
	PowerManagementCapabilities []uint16
	ErrorCleared                bool
	ErrorDescription            string
	LastErrorCode               uint32
	PNPDeviceID                 string
	PowerManagementSupported    bool
	StatusInfo                  uint16
	SystemCreationClassName     string
	SystemName                  string
	IsLocked                    bool
	Bandwidth                   uint32
	DisplayType                 uint16
	ScreenHeight                uint32
	ScreenWidth                 uint32
}

func MonitorInfo() (monitorInfo []Win32_DesktopMonitor, err error) {
	err1 := wmi.Query("Select * from Win32_DesktopMonitor", &monitorInfo)
	if err1 != nil {
		return nil, err1
	}
	return monitorInfo, nil
}
func JsonMonitorInfo() (f []byte, err error) {
	cpu, err := MonitorInfo()
	if err != nil {
		return f, err
	}
	t, jsonerr := json.Marshal(cpu)
	if jsonerr != nil {
		return f, jsonerr
	}
	return t, nil

}

func MapMonitorInfo() (data []map[string]interface{}, err error) {
	//data := make([]map[string]interface{}, 0)
	t, err1 := JsonMonitorInfo()
	if err1 != nil {
		return data, err1
	}
	json.Unmarshal(t, &data)
	cfg, err1 := ini.Load("./info/monitorconfig.ini")
	if err1 != nil {
		return data, err
	}
	list := []string{"显视器种类", "错误代码"}
	for _, v := range data {
		for _, vl := range list {
			if a, ok := v[vl].(float64); ok {
				v[vl] = cfg.Section(cfg.Section("").Key(vl).String()).Key(strconv.Itoa(int(a))).String()
			} else {
				continue
			}

		}
		//fmt.Println(cfg.Section(d.String()).Key(fmt.Sprintf("%v", v1)))
	}
	return data, nil

}
